package com.lee.lease.web.app.service.service.impl;


import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lee.lease.model.entity.LeaseAgreement;
import com.lee.lease.model.enums.ItemType;
import com.lee.lease.web.app.mapper.*;
import com.lee.lease.web.app.service.LeaseAgreementService;
import com.lee.lease.web.app.vo.agreement.AgreementDetailVo;
import com.lee.lease.web.app.vo.agreement.AgreementItemVo;
import com.lee.lease.web.app.vo.graph.GraphVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author liubo
 * @description 针对表【lease_agreement(租约信息表)】的数据库操作Service实现
 * @createDate 2023-07-26 11:12:39
 */
@Service
public class LeaseAgreementServiceImpl extends ServiceImpl<LeaseAgreementMapper, LeaseAgreement>
        implements LeaseAgreementService {

    @Autowired
    private LeaseAgreementMapper leaseAgreementMapper;
    @Autowired
    private ApartmentInfoMapper apartmentInfoMapper;
    @Autowired
    private GraphInfoMapper graphInfoMapper;
    @Autowired
    private RoomInfoMapper roomInfoMapper;
    @Autowired
    private PaymentTypeMapper paymentTypeMapper;
    @Autowired
    private LeaseTermMapper leaseTermMapper;


    @Override
    public List<AgreementItemVo> getListItem(String phone) {
        return leaseAgreementMapper.getListItem(phone);
    }

    @Override
    public AgreementDetailVo getDetailById(Integer id) {
        LeaseAgreement leaseAgreement = leaseAgreementMapper.selectById(id);
        if (leaseAgreement==null) {
            return null;
        }
        AgreementDetailVo agreementDetailVo = new AgreementDetailVo();
        BeanUtils.copyProperties(leaseAgreement,agreementDetailVo);
        String apartmentName = apartmentInfoMapper.findApartmentName(leaseAgreement.getApartmentId());
        List<GraphVo> apartmentGraphVoList = graphInfoMapper.getApartmentGraphVoList(ItemType.APARTMENT,leaseAgreement.getApartmentId());
        String roomNumber = roomInfoMapper.findRoomNumber(leaseAgreement.getRoomId());
        List<GraphVo> roomGraphVoList = graphInfoMapper.getRoomGraphVoList(ItemType.ROOM,leaseAgreement.getRoomId());
        String paymentTypeName = paymentTypeMapper.getPaymentTypeName(leaseAgreement.getPaymentTypeId());
        Integer leaseTermMountCount = leaseTermMapper.getLeaseTermMonthCount(leaseAgreement.getLeaseTermId());
        String leaseTermUnit = leaseTermMapper.getLeaseTermUnit(leaseAgreement.getLeaseTermId());

        //组装
        agreementDetailVo.setApartmentName(apartmentName);
        agreementDetailVo.setApartmentGraphVoList(apartmentGraphVoList);
        agreementDetailVo.setRoomNumber(roomNumber);
        agreementDetailVo.setRoomGraphVoList(roomGraphVoList);
        agreementDetailVo.setPaymentTypeName(paymentTypeName);
        agreementDetailVo.setLeaseTermMonthCount(leaseTermMountCount);
        agreementDetailVo.setLeaseTermUnit(leaseTermUnit);


        return agreementDetailVo;
    }
}




